/**
 * 首页根目录的Controller
 * http://localhost:8080/
 */
package controllers

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"soccerStar/services"
	"strconv"
)

type IndexController struct {
	Service services.SuperstarService
}

// http://localhost:8080/
func (c *IndexController) Get(ctx *gin.Context) {
	//c.Service.GetAll()
	//return mvc.Response{
	//	Text:"ok\n",
	//}
	datalist := c.Service.GetAll()
	//var datalist []models.StarInfo
	//set the model and render the view template.
	ctx.HTML(200, "indexIndex.html", gin.H{
		"Title":    "球星库",
		"Datalist": datalist,
	})
	//ctx.JSON(200,datalist)
}

// http://localhost:8080/{id}
func (c *IndexController) GetBy(ctx *gin.Context) {
	id, _ := strconv.Atoi(ctx.Query("id"))
	if id < 1 {
		ctx.Redirect(http.StatusMovedPermanently, "/")
	}
	data := c.Service.Get(id)
	ctx.HTML(200, "indexInfo.html", gin.H{
		"Title": "球星库",
		"info":  data,
	})
}

// http://localhost:8080/search?country=巴西
func (c *IndexController) GetSearch(ctx *gin.Context) {
	country := ctx.Query("country")
	datalist := c.Service.Search(country)
	// set the model and render the view template.
	ctx.HTML(200, "indexIndex.html", gin.H{
		"Title":    "球星库",
		"Datalist": datalist,
	})
}
